﻿using Microsoft.AspNetCore.Mvc;
using Model;
using Service.System.IService;
using StackExchange.Profiling;
using System.IO;
using WebApiCore.Controllers;

namespace WebApiCoreDemo.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class DepartmentController : BaseController
    {
        private readonly IDepartmentService DepartmentService;

        public DepartmentController(IDepartmentService DepartmentService)
        {
            this.DepartmentService = DepartmentService;
        }

        [HttpGet]
        public IActionResult GetList([FromQuery] PagerInfo pagerInfo)
        {
            var list = DepartmentService.GetDepartmentList(pagerInfo);
            return SUCCESS(list);
        }

        [HttpPost("export")]
        public async Task<IActionResult> Export()
        {
            var list = DepartmentService.GetDepartmentList();

            MemoryStream stream = ExportExcel(list, "Department", "单位信息");
            //return new FileStreamResult(stream, "application/octet-stream") { FileDownloadName = "单位信息.xlsx" };
            return new FileStreamResult(stream, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
            {
                FileDownloadName = "单位信息.xlsx"
            };

            //return SUCCESS(new { path = "/export/" + sFileName, fileName = sFileName });
        }

        /// <summary>
        /// 获取MiniProfiler的html片段
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Route("GetHtml")]
        public IActionResult GetHtml()
        {
            var html = MiniProfiler.Current.RenderIncludes(HttpContext);
            return Ok(html.Value);
        }
    }
}
